package thread;

// 懒汉模式
class SingletonLazy {
    private static volatile SingletonLazy instance = null;//这里首先将 instance 声明并初始化为 null，此时对象并没有被真正创建出来，
    // 只是声明了一个静态变量并赋予了初始的 null 值
    private static Object locker = new Object();

    public static SingletonLazy getInstance() {
        if (instance == null) {//判断是否需要加锁
            synchronized (locker) {
                if (instance == null) {//判断是否需要创建对象
                    instance = new SingletonLazy();
                }
            }
        }

        return instance;
    }

    private SingletonLazy() {
    }
}

public class Demo28 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}
